package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqFlocculationProduceFaultMapper;
import com.guofen.pojo.SjqFlocculationProduceFault;
import com.guofen.service.sjq.SjqFlocculationProduceFaultService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqFlocculationProduceFaultServiceImpl implements SjqFlocculationProduceFaultService {

    @Autowired
    private SjqFlocculationProduceFaultMapper sjqFlocculationProduceFaultMapper;

    @Override
    public boolean addForm(String equipmentName, String downtime, String operate_person, String fault_description,
                           String start_time, String end_time, String personIncharge, String repair_person,
                           String fault_analysis, String repair_method, String method_person, String recovery_time,
                           String check_person) {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();

        SjqFlocculationProduceFault sjqFlocculationProduceFault = new SjqFlocculationProduceFault();
        sjqFlocculationProduceFault.setDate(date);
        sjqFlocculationProduceFault.setEquipmentname(equipmentName);
        sjqFlocculationProduceFault.setDowntime(downtime);
        sjqFlocculationProduceFault.setOperatePerson(operate_person);
        sjqFlocculationProduceFault.setFaultAnalysis(fault_analysis);
        sjqFlocculationProduceFault.setStartTime(start_time);
        sjqFlocculationProduceFault.setEndTime(end_time);
        sjqFlocculationProduceFault.setPersonincharge(personIncharge);
        sjqFlocculationProduceFault.setRepairPerson(repair_person);
        sjqFlocculationProduceFault.setFaultAnalysis(fault_analysis);
        sjqFlocculationProduceFault.setRepairMethod(repair_method);
        sjqFlocculationProduceFault.setMethodPerson(method_person);
        sjqFlocculationProduceFault.setRecoveryTime(recovery_time);
        sjqFlocculationProduceFault.setCheckPerson(check_person);
        int result = sjqFlocculationProduceFaultMapper.insert(sjqFlocculationProduceFault);
        if (result == 1){
            //该故障设备 sjq_flocculation_produce_check --> is_form = 1
            sjqFlocculationProduceFaultMapper.updateIsForm(equipmentName);
            return true;
        }else {
            return false;
        }

    }

    @Override
    public SjqFlocculationProduceFault findForm() {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();
        System.out.println("当前日期" + date);
        //获取最新记录
        SjqFlocculationProduceFault sjqFlocculationProduceFault = sjqFlocculationProduceFaultMapper.findLast();
        System.out.println("最新记录" + sjqFlocculationProduceFaultMapper.findLast());
        //获取最新记录日期
        String dateLast =  sjqFlocculationProduceFault.getDate();
        System.out.println("最新记录日期" + dateLast);

        if (date.equals(dateLast))
            return sjqFlocculationProduceFault;
        else
            return null;
    }

    @Override
    public List<String> findFaultEquipList() {
        return sjqFlocculationProduceFaultMapper.findFaultEquipList();
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqFlocculationProduceFault> list = sjqFlocculationProduceFaultMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqFlocculationProduceFault> list = sjqFlocculationProduceFaultMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqFlocculationProduceFaultMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqFlocculationProduceFault record) {
        return sjqFlocculationProduceFaultMapper.updateByPrimaryKeySelective(record);
    }
}
